package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysFillRuleRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysFillRuleDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 填值规则
 */
@Slf4j
@Api(tags = "填值规则")
@RestController
@RequestMapping("/sys/fillRule")
public class SysFillRuleController {

    private SysFillRuleRestAPI sysFillRuleRestAPI;

    /**
     * 分页列表查询
     *
     * @param sysFillRuleDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "填值规则-分页列表查询", notes = "填值规则-分页列表查询")
    @GetMapping(value = "/list")
    public MatApiResponse<?> queryPageList(SysFillRuleDTO sysFillRuleDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.queryPageList(sysFillRuleDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 测试 ruleCode
     *
     * @param ruleCode
     * @return
     */
    @GetMapping(value = "/testFillRule")
    public MatApiResponse testFillRule(@RequestParam("ruleCode") String ruleCode) {
        MatApiResponse matApiResponse = sysFillRuleRestAPI.testFillRule(ruleCode);
        return matApiResponse;
    }

    /**
     * 添加
     *
     * @param sysFillRuleDTO
     * @return
     */
    @ApiOperation(value = "填值规则-添加", notes = "填值规则-添加")
    @PostMapping(value = "/add")
    public MatApiResponse<?> add(@RequestBody SysFillRuleDTO sysFillRuleDTO) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.add(sysFillRuleDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *
     * @param sysFillRuleDTO
     * @return
     */
    @ApiOperation(value = "填值规则-编辑", notes = "填值规则-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<?> edit(@RequestBody SysFillRuleDTO sysFillRuleDTO) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.edit(sysFillRuleDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "填值规则-通过id删除", notes = "填值规则-通过id删除")
    @DeleteMapping(value = "/delete")
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "填值规则-批量删除", notes = "填值规则-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public MatApiResponse<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "填值规则-通过id查询", notes = "填值规则-通过id查询")
    @GetMapping(value = "/queryById")
    public MatApiResponse<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 导出excel
     *
     * @param sysFillRuleDTO
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysFillRuleDTO sysFillRuleDTO) {
        ModelAndView modelAndView = sysFillRuleRestAPI.exportXls(sysFillRuleDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        MatApiResponse<?> matApiResponse = sysFillRuleRestAPI.importExcel();
        return matApiResponse;
    }

    /**
     * 通过 ruleCode 执行自定义填值规则
     *
     * @param ruleCode 要执行的填值规则编码
     * @param formData 表单数据，可根据表单数据的不同生成不同的填值结果
     * @return 运行后的结果
     */
    @PutMapping("/executeRuleByCode/{ruleCode}")
    public MatApiResponse executeByRuleCode(@PathVariable("ruleCode") String ruleCode, @RequestBody JSONObject formData) {
        MatApiResponse matApiResponse = sysFillRuleRestAPI.executeByRuleCode(ruleCode, formData);
        return matApiResponse;
    }


    /**
     * 批量通过 ruleCode 执行自定义填值规则
     *
     * @param ruleData 要执行的填值规则JSON数组：
     *                 示例： { "commonFormData": {}, rules: [ { "ruleCode": "xxx", "formData": null } ] }
     * @return 运行后的结果，返回示例： [{"ruleCode": "order_num_rule", "result": "CN2019111117212984"}]
     */
    @PutMapping("/executeRuleByCodeBatch")
    public MatApiResponse executeByRuleCodeBatch(@RequestBody JSONObject ruleData) {
        MatApiResponse matApiResponse = sysFillRuleRestAPI.executeByRuleCodeBatch(ruleData);
        return matApiResponse;
    }

}